package com.qmx.launcher.activity.general;

import android.app.Activity;
import android.content.Context;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.open.androidtvwidget.bridge.RecyclerViewBridge;
import com.open.androidtvwidget.leanback.adapter.GeneralAdapter;
import com.open.androidtvwidget.leanback.recycle.LinearLayoutManagerTV;
import com.open.androidtvwidget.leanback.recycle.RecyclerViewTV;
import com.open.androidtvwidget.view.MainUpView;
import com.qmx.launcher.R;
import com.qmx.launcher.adapter.SleepOrScreenRecyclerViewPresenter;

public class LanguageActivity extends Activity implements RecyclerViewTV.OnItemListener {
    private Context mContext;
    private RecyclerViewTV mRecyclerView;
    private MainUpView mainUpView1;
    private RecyclerViewBridge mRecyclerViewBridge;
    private View oldView;
    private SleepOrScreenRecyclerViewPresenter mRecyclerViewPresenter;
    private GeneralAdapter mGeneralAdapter;
    private int mSavePos = 0;
    private String str[] = new String[]{"中文(简体)", "English"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_language);

        mContext = this;
        mRecyclerView = (RecyclerViewTV) findViewById(R.id.recyclerView);
        mainUpView1 = (MainUpView) findViewById(R.id.mainUpView1);
        mainUpView1.setEffectBridge(new RecyclerViewBridge());
        // 注意这里，需要使用 RecyclerViewBridge 的移动边框 Bridge.
        mRecyclerViewBridge = (RecyclerViewBridge) mainUpView1.getEffectBridge();
        mRecyclerViewBridge.setUpRectResource(R.drawable.white_light_10);
        float density = getResources().getDisplayMetrics().density;
        RectF receF = new RectF(getDimension(R.dimen.w_10) * density, getDimension(R.dimen.h_15) * density,
                getDimension(R.dimen.w_30) * density, getDimension(R.dimen.h_15) * density);
        mRecyclerViewBridge.setDrawUpRectPadding(receF);
        recyclerViewLinerLayout(RecyclerView.VERTICAL);
        //
        mRecyclerView.setOnItemListener(this);
        // item 单击事件处理.
        mRecyclerView.setOnItemClickListener(new RecyclerViewTV.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerViewTV parent, View itemView, int position) {
                Toast.makeText(mContext, "点击位置" + position, Toast.LENGTH_SHORT).show();
                for (int i = 0; i < mRecyclerViewPresenter.hashMap.size(); i++) {
                    if (i == position) {
                        mRecyclerViewPresenter.hashMap.put(i, true);
                    } else {
                        mRecyclerViewPresenter.hashMap.put(i, false);
                        Log.d("setOnItemClickListener", i + "---------- false");
                    }
                }
                mRecyclerViewPresenter.setData(str);
//                mGeneralAdapter.notifyDataSetChanged();
            }
        });
    }

    public float getDimension(int id) {
        return getResources().getDimension(id);
    }

    /**
     * 测试LinerLayout.
     */
    private void recyclerViewLinerLayout(int orientation) {
        LinearLayoutManagerTV layoutManager = new LinearLayoutManagerTV(this);
        layoutManager.setOrientation(orientation);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setFocusable(false);
        mRecyclerViewPresenter = new SleepOrScreenRecyclerViewPresenter(str);
        mGeneralAdapter = new GeneralAdapter(mRecyclerViewPresenter);
        mRecyclerView.setAdapter(mGeneralAdapter);
        mRecyclerView.setDefaultSelect(mSavePos);
    }

    @Override
    public void onItemPreSelected(RecyclerViewTV parent, View itemView, int position) {
        mRecyclerViewBridge.setUnFocusView(oldView);
    }

    @Override
    public void onItemSelected(RecyclerViewTV parent, View itemView, int position) {
        mRecyclerViewBridge.setFocusView(itemView, 1.01f);
        oldView = itemView;
    }

    @Override
    public void onReviseFocusFollow(RecyclerViewTV parent, View itemView, int position) {
        mRecyclerViewBridge.setFocusView(itemView, 1.01f);
        oldView = itemView;
    }
}
